<template>
  <div id="taskZxEchars" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme


export default {
  name: 'taskZxEchars',
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '250px'
    },
    dataInfo:{
      default: null
    }
  },
  watch: {
    dataInfo: function(newVal,oldVal){
          this.init()
      }
  },
  mounted() {
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy(){
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    init() {
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        toolbox: {
          feature: {
            //saveAsImage: {}
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'time',
         // data: this.cpuInfoChart.cpuTimeData
        }],
        yAxis: [{
          type: 'value'
        }],
        series: [{
          data: this.dataInfo,
          /*[["2020-12-25 15:33:11","2"],["2020-12-25 15:34:11","1"],
          ["2020-12-25 15:35:11","7"],["2020-12-25 15:36:11","4"],
          ["2020-12-25 15:37:11","2"],["2020-12-25 15:38:11","2"],
          ["2020-12-25 15:39:11","1"],["2020-12-25 15:40:11","8"],
          ["2020-12-25 15:41:11","10"],["2020-12-25 15:42:11","4"],
          ["2020-12-25 15:43:11","4"],["2020-12-25 15:44:11","2"],
          ["2020-12-25 15:45:11","5"],["2020-12-25 15:46:11","3"],
          ["2020-12-25 15:47:11","3"],["2020-12-25 15:48:11","3"],
          ["2020-12-25 15:49:11","0"],["2020-12-25 15:50:11","3"],
          ["2020-12-25 15:51:11","3"],["2020-12-25 15:52:11","3"],
          ["2020-12-25 15:53:11","6"],["2020-12-25 15:54:11","3"],
          ["2020-12-25 15:55:11","1"],["2020-12-25 15:56:11","3"],
          ["2020-12-25 15:57:11","12"],["2020-12-25 15:58:11","3"],
          ["2020-12-25 15:59:11","4"],["2020-12-25 16:00:11","3"],
          ["2020-12-25 16:01:11","9"],["2020-12-25 16:02:11","3"]],*/
          type: 'line'
          //this.cpuInfoChart.cpuTimeData,
        }]
      }
      const ServiceRequestsCpu = echarts.init(document.getElementById('taskZxEchars'))
      ServiceRequestsCpu.setOption(option)
      window.addEventListener('resize', function() {
        ServiceRequestsCpu.resize()
      })
    },
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
  }
}
</script>
